package com.wiegand.gui.button {
	import com.caurina.transitions.Tweener;
	import com.wiegand.event.StatusEvent;
	import com.wiegand.gui.button.views.ButtonViewClasses;
	import com.wiegand.sound.Soundcontroller;
	import com.wiegand.util.ColorRectangle;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 * @author kwiegand
	 */
	 
	public class MetaNavButton extends Button{

		private var _bt : Sprite;

		private var _down : MovieClip;
		private var _up : MovieClip;
		private var _over : MovieClip;
		private var _overMask : Sprite;
		private var _overText : Sprite;
		private var _hit : Sprite;
		
		public static var BUTTON_STATUS : String = "button status";
		public static var EVENT_OPEN : String = "button open";

		public function MetaNavButton( xml : XMLList ):void{
			super( xml ); 			
		}
				
		override protected function init():void{
			_bt = new Sprite();
			_bt.name = super._name;
			
			name = super._xml.@name;
			
			var viewClass : Class = ButtonViewClasses.getClass( super._viewName );
			_view = new viewClass();
			
			_down = new MovieClip();
			_down.name = "down";
			_down = _view.getDownState( super._xml );
			_down.alpha = 0;
			_bt.addChild( _down );
						
			_up = new MovieClip();
			_up.name = "up";
			_up = _view.getUpState(super._xml);
			_bt.addChild( _up );
			
			_over = new MovieClip();
			_over.name = "over";
			_over = _view.getOverState(super._xml);
			_over.alpha = 0;
			_bt.addChild( _over );
			
			_overMask = Sprite(_over.getChildByName("recMask"));
			_overText = Sprite(_over.getChildByName("btText"));
			
			_hit = ColorRectangle.create( 0xff0000, 0 );
			_hit.buttonMode = true;
			_hit.useHandCursor = true;
			_hit.width = _bt.width;
			_hit.height = _bt.height;			
			_bt.addChild( _hit );
			
			_hit.addEventListener( MouseEvent.MOUSE_DOWN, down);
			_hit.addEventListener( MouseEvent.MOUSE_UP, up);
			_hit.addEventListener( MouseEvent.MOUSE_OVER, over);
			_hit.addEventListener( MouseEvent.MOUSE_OUT, out);
			
			addChild( _bt );	
							
		}
		
		public function disable():void{
			_hit.buttonMode = false;
			_hit.removeEventListener( MouseEvent.MOUSE_DOWN, down);
			_hit.removeEventListener( MouseEvent.MOUSE_UP, up);
			_hit.removeEventListener( MouseEvent.MOUSE_OVER, over);
			_hit.removeEventListener( MouseEvent.MOUSE_OUT, out);		
		}
		
		public function enable():void{
			_hit.buttonMode = true;
			_hit.addEventListener( MouseEvent.MOUSE_DOWN, down);
			_hit.addEventListener( MouseEvent.MOUSE_UP, up);
			_hit.addEventListener( MouseEvent.MOUSE_OVER, over);
			_hit.addEventListener( MouseEvent.MOUSE_OUT, out);		
		}
		
		public function open( delay : Number = 0 ):void{
			var foo_y : Number = this.y;
			this.y = this.y + this.height/2;			
			this.scaleY = 0.1;
			Tweener.removeTweens( this );
			Tweener.addTween( this, { alpha:1, scaleY:1, y:foo_y, delay:1.5+delay, time:0.5, transition:"easeoutbounce", onComplete:callOpen } );
		}
		
		private function callOpen():void{
			dispatchEvent( new StatusEvent( EVENT_OPEN, "open") );
		}
				
		private function dispatchStatus( e : MouseEvent ) : void {
			dispatchEvent( new StatusEvent( BUTTON_STATUS, e.type ) );
		}
		
		private function down( e : MouseEvent ) : void {
			
			super.click();
			
			_up.alpha = 0;
			_over.alpha = 1;
			_down.alpha = 0;
			_overMask.alpha = 0;
			stopTweens();
			Tweener.addTween( _overMask, { alpha:1, time:.5, transition:"expo" } );
			dispatchStatus( e );
			
			if(_xml.@clickSnd != "" && _xml.@clickSnd != null ) Soundcontroller.getInstance().playEffSound(_xml.@clickSnd);
			
		}
		private function up( e : MouseEvent ) : void {
			_up.alpha = 0;
			_over.alpha = 1;
			_down.alpha = 0;
		}
		
		private function over( e : MouseEvent ) : void {
			_up.alpha = 0;
			_over.alpha = 1;
			_down.alpha = 0;
			_overMask.alpha = 1;
			stopTweens();
			Tweener.addTween( _overMask, { y:0, scaleX:1, scaleY:1, time:.5, transition:"easeoutexpo" } );
			dispatchStatus( e );
			if(_xml.@clickSnd != "" && _xml.@clickSnd != null ) Soundcontroller.getInstance().playEffSound(_xml.@overSnd);
		}
		
		private function out( e : MouseEvent ) : void {
			_up.alpha = 0;
			_over.alpha = 1;
			_down.alpha = 0;
			_overMask.alpha = 1;
			stopTweens();
			Tweener.addTween( _overMask, { y:_overText.height, scaleX:0.001, scaleY:0.001, time:.5, transition:"easeoutexpo" } );
			dispatchStatus( e );
		}
		
		private function stopTweens():void{
			Tweener.removeTweens( _up );
			Tweener.removeTweens( _over );
			Tweener.removeTweens( _down );
		}
				
		override public function destroy():void{
			stopTweens();	
			removeChild( _bt );
			_hit.removeEventListener( MouseEvent.MOUSE_DOWN, down);
			_hit.removeEventListener( MouseEvent.MOUSE_UP, up);
			_hit.removeEventListener( MouseEvent.MOUSE_OVER, over);
			_hit.removeEventListener( MouseEvent.MOUSE_OUT, out);
			super.destroy();
		}
						
	}
	
}
